using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using SapaFinance.DTO;
using SapaFinance.Domain;
using SapaFinance.Domain.Model;
using SapaFinance.GenericRepository;
using SapaFinance.Infrastructure;

namespace SapaFinance.Services
{
    public interface IUserService {
        User GetUser(string login);
        User GetUser(Guid id);
        IEnumerable<UserDTO> GetListDTO();
        void Insert(UserDTO dto);
        void Delete(Guid id);
        void Update(UserDTO dto);
        IEnumerable<SelectListItem> GetSelectedListById(Guid id);
    }

    public class UserService : IUserService
    {
        private readonly IGenericRepository<User, Guid> _userRepository;
        private readonly IMappingEngine _mappingEngine;
        private readonly IEntityFactory _entityFactory;
        private readonly IUnitOfWork _unitOfWork;

        public UserService(IGenericRepository<User, Guid> userRepository, IMappingEngine mappingEngine, IEntityFactory entityFactory, IUnitOfWork unitOfWork)
        {
            _userRepository = userRepository;
            _mappingEngine = mappingEngine;
            _entityFactory = entityFactory;
            _unitOfWork = unitOfWork;
        }

        #region ICurrencyService Members

      

        public User GetUser(string login)
        {
            return _userRepository.Specify<IUserSpecification>().WithLogin(login.WithoutDomain()).ToResult().SingleOrDefault();
        }

        public User GetUser(Guid id)
        {
            return _userRepository.GetById(id);
        }
        public IEnumerable<UserDTO> GetListDTO()
        {
            return _mappingEngine.Map<IEnumerable<User>, IEnumerable<UserDTO>>(_userRepository.GetAll());
        }
        public virtual void Insert(UserDTO dto)
        {
            var entity = _entityFactory.BuildUser(dto);
            _userRepository.Insert(entity);
            _unitOfWork.Flush();
        }

        public virtual void Delete(Guid id)
        {
            var entity = _userRepository.GetById(id);
            _userRepository.Delete(entity);
            _unitOfWork.Flush();
        }

        public virtual void Update(UserDTO dto)
        {
            var user = _userRepository.GetById(dto.UID);
            user.FullName = dto.FullName;
            user.Login = user.Login;
            _userRepository.Update(user);
            _unitOfWork.Flush();
        }

        public IEnumerable<SelectListItem> GetSelectedListById(Guid id)
        {
      
            var result = new List<SelectListItem> { };
            var users = _userRepository.GetAll();
            result.AddRange(_mappingEngine.Map<IEnumerable<User>, IEnumerable<SelectListItem>>(users));
            foreach (var item in result.Where(item => item.Value.Equals(id.ToString())))
            {
                item.Selected = true;
            }
            return result;
        }

        #endregion
    }

  
}